﻿module Sustainalytics.Authentication {

    export class UserInfoService implements IUserInfoService {

        private userInfo: IUserInfo;

        constructor() {
        }

        public setUserInfo(userInfo: IUserInfo): void {
            localStorage.setItem('sust_userInfo', JSON.stringify(userInfo));
            this.userInfo = userInfo;
        }

        public getUserInfo(): IUserInfo {
            if (!this.userInfo) {
                this.userInfo = localStorage.getItem('sust_userInfo');
            }

            if (!this.userInfo) {
                this.userInfo = this.createEmptyUserInfo();
            }

            return this.userInfo;
        }

        private createEmptyUserInfo() {

            return { username: null, email: null, name: null, isAuthenticated: false, userId: null, accountId: null };
        }
    }


    export var userInfoServiceName = 'UserInfoService';

    angular.module(authenticationModuleName).service(userInfoServiceName, UserInfoService);
} 